import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testclusterrole:

    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/clusterrole/1_get_clusterrole_list.yaml'))
    def test_get_clusterrole_list(self, clusterroleinfo, get_env_info):
        # 获取clusterrole列表接口
        baseUrl = get_env_info
        url = baseUrl + clusterroleinfo['request']['url'].replace('{namespace}','default')
        method = clusterroleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/clusterrole/2_create_clusterrole.yaml'))
    def test_create_clusterrole(self, clusterroleinfo, get_env_info):
        # 创建clusterrole接口
        baseUrl = get_env_info
        url = baseUrl + clusterroleinfo['request']['url'].replace('{namespace}','default')
        method = clusterroleinfo['request']['method']
        data = clusterroleinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '不正确' not in clusterroleinfo['name']:
            clusterrolename=req.json()['metadata']['name']
            globals()['clusterrolename']=clusterrolename
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/clusterrole/3_get_specific_clusterrole.yaml'))
    def test_get_specific_clusterrole(self, clusterroleinfo, get_env_info):
        # 获取指定clusterrole接口
        baseUrl = get_env_info
        url = baseUrl + clusterroleinfo['request']['url'].replace('{namespace}','default').replace('{clusterrolename}',globals()['clusterrolename'])
        method = clusterroleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '不正确' not in clusterroleinfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/clusterrole/4_update_clusterrole.yaml'))
    def test_update_clusterrole(self, clusterroleinfo, get_env_info):
        # 更新指定clusterrole接口
        baseUrl = get_env_info
        url = baseUrl + clusterroleinfo['request']['url'].replace('{namespace}','default').replace('{clusterrolename}',globals()['clusterrolename'])
        method = clusterroleinfo['request']['method']
        clusterroleinfo['request']['data'] = globals()['updatereqbody']
        data = clusterroleinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/clusterrole/5_update_section_clusterrole.yaml'))
    def test_update_section_clusterrole(self, clusterroleinfo, get_env_info):
        # 部分更新指定clusterrole的接口
        baseUrl = get_env_info
        url = baseUrl + clusterroleinfo['request']['url'].replace('{namespace}','default').replace('{clusterrolename}',globals()['clusterrolename'])
        method = clusterroleinfo['request']['method']
        data = clusterroleinfo['request']['data']
        header = clusterroleinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('clusterroleinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/clusterrole/6_delete_specific_clusterrole.yaml'))
    def test_delete_specific_clusterrole(self, clusterroleinfo, get_env_info):
        # 删除clusterrole接口
        baseUrl = get_env_info
        url = baseUrl + clusterroleinfo['request']['url'].replace('{namespace}','default').replace('{clusterrolename}',globals()['clusterrolename'])
        method = clusterroleinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        # print(req.content)
        assert req.status_code == clusterroleinfo['validate'][0]['equals']['status_code']